<?php
/**
 * +----------------------------------------------------------------------
 * | 九月科技-ztuc.cn
 * +----------------------------------------------------------------------
 *                      .::::.
 *                    .::::::::.            | AUTHOR: siyu
 *                    :::::::::::           | EMAIL: ztucke@ztuc.cn
 *                 ..:::::::::::'           | DATETIME: 2020/01/31
 *             '::::::::::::'
 *                .::::::::::
 *           '::::::::::::::..
 *                ..::::::::::::.
 *              ``::::::::::::::::
 *               ::::``:::::::::'        .:::.
 *              ::::'   ':::::'       .::::::::.
 *            .::::'      ::::     .:::::::'::::.
 *           .:::'       :::::  .:::::::::' ':::::.
 *          .::'        :::::.:::::::::'      ':::::.
 *         .::'         ::::::::::::::'         ``::::.
 *     ...:::           ::::::::::::'              ``::.
 *   ```` ':.          ':::::::::'                  ::::..
 *                      '.:::::'                    ':'````..
 * +----------------------------------------------------------------------
 */
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2020/3/14
 * Time: 17:18
 */

namespace app\store\model;
use app\store\controller\Upload;
use app\store\model\core\InitModel;
use think\model\concern\SoftDelete;


class Image extends InitModel
{
    use SoftDelete;
    protected $deleteTime = 'delete_time';
    protected $defaultSoftDelete = 0;
    // 定义表
    protected $name = 'images';

    // 第一自动写入时间字段
    protected $createTime = 'create_time';


    // 可用字段
    protected static $available = [
        'id',
        'table_id',
        'url',
        'types',
        'status',
        'create_time',
    ];

    /**
     * 获取图片
     * @param int $tableId
     * @param int $type
     * @return string
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     * @author: MK
     * @Time: 2020/4/5 13:01
     */
    public static function image(int $tableId ,int $type)
    {
        // force强制索引
        $img = self::where(['table_id'=>$tableId,'types'=>$type])->force('images')->value('url');      
        return Upload::image($img);
    }
    public static function get_info()
    {
         $data = self::order(['id'=>'desc'])->where(['status' => 1])->where('table_id', '>', 0)->paginate(8);
         return $data;
    }
    /**
     * 新增和编辑
     * @param $post
     * @return bool
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     * @author: MK
     * @Time: 2020/4/5 13:00
     */
    public static function add($post)
    {
        if (!$post) return false;

        $model = self::where(['table_id'=>$post['table_id'],'types'=>$post['types']])->find();

        if ($model){
            // 编辑逻辑
            return $model->save($post);

        }else{
            // 新增逻辑
            $image = new self();
            foreach ($post as $key => $val) {
                if (in_array($key, self::$available)) {
                    $image->$key = $val;
                }
            }
           return $image->save();
        }
    }
}